package org.neowei.image.filter;

import java.awt.image.BufferedImage;

public class HorizontalFilter implements Runnable {
	private final BufferedImage outimage;
	private final BufferedImage srcimage;
	private final int startX;
	private final int stopX;
	private final int start;
	private final int x;
	private final double[] normalContrib;
	private final int srcheight;

	public HorizontalFilter(BufferedImage outimage, BufferedImage srcimage, int startX, int stopX, int start, int x, double[] normalContrib, int srcheight) {
		this.outimage = outimage;
		this.srcimage = srcimage;
		this.startX = startX;
		this.stopX = stopX;
		this.start = start;
		this.x = x;
		this.normalContrib = normalContrib;
		this.srcheight = srcheight;
	}

	@Override
	public void run() {
		for (int y = 0; y <= srcheight; y++) {
			double valueRed = 0, valueGreen = 0, valueBlue = 0;
			for (int i = startX, j = start; i <= stopX; i++, j++) {
				int valueRGB = srcimage.getRGB(i, y);
				double contributor = normalContrib[j];
				valueRed = Filter.getRedValue(valueRGB, valueRed, contributor);
				valueGreen = Filter.getGreenValue(valueRGB, valueGreen, contributor);
				valueBlue = Filter.getBlueValue(valueRGB, valueBlue, contributor);
			}
			outimage.setRGB(x, y, Filter.comRGB(Filter.clip((int) valueRed), Filter.clip((int) valueGreen), Filter.clip((int) valueBlue)));
		}
	}
}
